package com.gin.fanbox.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;

/**
 * queryString工具类
 *
 * @author bx002
 * @since 2024/2/6 10:50
 */
public class QsUtils {
    /**
     * 将queryString解析为Map对象
     *
     * @param queryString queryString
     * @return Map
     */
    public static LinkedHashMap<String, String> parseParameterMap(String queryString) {
        final LinkedHashMap<String, String> map = new LinkedHashMap<>();
        try {
            queryString = URLDecoder.decode(queryString, "utf-8");
            if (queryString.contains("?")) {
                queryString = queryString.substring(queryString.indexOf("?") + 1);
            }
            final String[] pairs = queryString.split("&");
            for (String pair : pairs) {
                final String[] s = pair.split("=");
                map.put(s[0], s[1]);
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return map;
    }
}
